package com.itheima.stock.controller;

import com.alibaba.excel.EasyExcel;
import com.itheima.stock.api.StockControllerAPI;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.StockInfoExcelDTO;
import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.response.PageResult;
import com.itheima.stock.response.ResponseResult;
import com.itheima.stock.service.StockService;
import com.itheima.stock.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liwen
 */
@RestController
public class StockController implements StockControllerAPI {

    private final StockService stockService;

    public StockController(StockService stockService) {
        this.stockService = stockService;
    }

    /**
     * 统计指定时间个股涨跌数量统计
     * @return
     */
    @Override
    public ResponseResult<StockUpDownVO<InfoVO>> stockUpDown() {
        return ResponseResult.ok(stockService.stockUpDown());
    }

    @Override
    public ResponseResult<TradeAmtVO<OptionVO>> tradeAmt() {
        return ResponseResult.ok(stockService.tradeAmt());
    }

    @Override
    public void export(int page, int pageSize, HttpServletResponse response) throws IOException {

        // 设置响应数据类型
        response.setContentType("application/vnd.ms-excel");
        // 指定相应编码
        response.setCharacterEncoding("UTF-8");
        // 指定文件名称（处理中文乱码问题）
        String fileName = Constants.EXCEL_FILE_PREFIX + page + Constants.EXCEL_FILE_SUFFIX;
        URLEncoder.encode(fileName, StandardCharsets.UTF_8);
        // 使用EasyExcel实现文件导出功能
        AtomicInteger counter = new AtomicInteger(0);

        EasyExcel.write(response.getOutputStream(), StockInfoExcelDTO.class)
                .sheet("股票信息")
                .doWrite(stockService.getStockAll(page, pageSize).getRows().stream().map((vo)->{
                    StockInfoExcelDTO dto = new StockInfoExcelDTO();
                    BeanUtils.copyProperties(vo, dto);
                    dto.setNum(counter.incrementAndGet());
                    return dto;
                }).toList());

    }

    /**
     * 统计涨停和跌停的股票数据
     * @return
     */
    @Override
    public ResponseResult<UpDownVO<OptionVO>> upDown() {
        return ResponseResult.ok(stockService.upDown());
    }

    @Override
    public ResponseResult<PageResult<MoreVO>> getStockAll(Integer page, Integer pageSize) {
        return ResponseResult.ok(stockService.getStockAll(page, pageSize));
    }

    /**
     * 获取所有涨幅信息
     * @return
     */
    @Override
    public ResponseResult<List<IncreaseVO>> getStockIncrease() {
        return ResponseResult.ok(stockService.getStockIncrease());
    }

    /**
     * 获取所有板块信息
     * @return
     */
    @Override
    public ResponseResult<List<SectorAllVO>> sectorAll() {
        return ResponseResult.ok(stockService.sectorAll());
    }

    /**
     * 获取所有股票大盘信息
     * @return
     */
    @Override
    public ResponseResult<List<MarketIndexVO>> innerIndexAll() {
        return ResponseResult.ok(stockService.innerIndexAll());
    }

    /**
     * 获取所有股票经营业务信息列表
     * @return
     */
    @Override
    public ResponseResult<List<StockBusiness>> getStockBusiness() {
        List<StockBusiness> stockBusiness = stockService.getStockBusiness();
        return ResponseResult.ok(stockBusiness);
    }
}
